$(document).ready(function() {
    $("#entrar").click(function() {

        $("#msg_erro").html("");

        $.ajax({
            url: '/RRFrutas/Controller_Login',
            type: 'POST',
            data: {
                oper: "4",
                usuario: $("#usuario").val(),
                senha: $("#senha").val()

            },
            complete: function(e, xhr, result) {
                if (e.readyState == 4 && e.status == 200) {
                    try { //Converte a resposta HTTP JSON em um objeto JavaScript
                        var Obj = eval("(" + e.responseText + ")"); //Combo OS

                    }
                    catch (err) { //
          
                         $("#msg_erro").append("<h4 style='color:red;'>Nome de usuário ou senha inválidos.</h4>");

                    }
                    if (Obj != null) {
                    
             
                            var data = new Date();
                            data.setDate(data.getDate() + 1);
                            setCookie("id_usuario", Obj[1].id, data);
                            setCookie("usuario", Obj[1].usuario, data);
                            setCookie("senha", Obj[1].senha, data); 
                            setCookie("adm",Obj[1].adm,data);
                            setCookie("status",Obj[1].status, data);
                 
                            
                         
                            if (Obj[1].adm=== true){
                            window.location.href = "/RRFrutas/homeadm.html";
                            }else{
                              window.location.href = "/RRFrutas/home.html";  
                            }
         
                            
  
           
                           
         



                    }
                }

            }
        });


    });

});


// JavaScript Document
// Código de domínio público escrito por Bill Dortch
function setCookie(name, value) 
{
   	var argv = setCookie.arguments;
   	var argc = setCookie.arguments.length;
   	var expires = (argc > 2) ? argv[2] : null;
   	var path = (argc > 3) ? argv[3] : null;
   	var domain= (argc > 4) ? argv[4] : null;
   	var secure= (argc>5) ? argv[5]: false;
   	document.cookie = name+"="+escape(value)+ 
  	((expires==null) ? "" : (";  expires= "+ expires.toGMTString())) +
  	((path == null) ? "": ("; path=" + path)) +  
  	((domain==null)? "": ("; domain=" + domain)) +
  	((secure==true)? "; secure": "");
}
function getCookie(name) 
{
   	var arg = name + "=";
   	var alen = arg.length;
   	var clen= document.cookie.length;
   	var i=0;
    	while (i < clen) {
      	var j = i + alen;
      	if (document.cookie.substring(i,j) == arg) 
           		return getCookieVal(j);
      	i = document.cookie.indexOf(" ", i) + 1;
    		if  (i == 0) break;
  	 }
   	return null;
}

function getCookieVal(offset) 
{
   	var endstr = document.cookie.indexOf(";", offset);
   	if (endstr == -1) endstr = document.cookie.length;
   	return unescape(document.cookie.substring(offset, endstr));
}


function delCookie(name) 
{
  	 if (getCookie(name)) {
       	document.cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT";
   	}
}
